package com.jordi.tetris.model;

public abstract class Piece {

	private Matrix<Block> shape;
	private Position position;

	protected Piece(Matrix<Boolean> pattern) {
		shape = new Matrix<Block>(pattern.getRows(), pattern.getColumns());

		for (int i = 0; i < shape.getRows(); i++) {
			for (int j = 0; j < shape.getColumns(); j++) {
				if (pattern.get(i, j)) {
					shape.set(BlockFactory.createPieceBlock(this), i, j);
				} else {
					shape.set(BlockFactory.createEmptyBlock(), i, j);
				}
			}
		}
	}

	protected abstract String getName();

	@Override
	public String toString() {
		String str = "Piece : " + getName();
		if (position != null)
			str += " (" + getX() + "," + getY() + ")";
		return str;
	}

	public void rotate() {
		shape.transpose();
	}

	public Matrix<Block> getShape() {
		return shape;
	}

	public int getWidth() {
		return shape.getColumns();
	}

	public int getHeight() {
		return shape.getRows();
	}

	public Position getPosition() {
		return position;
	}

	public void setPosition(Position position) {
		this.position = position;
	}

	public int getX() {
		return position.x;
	}

	public int getY() {
		return position.y;
	}

	public void moveDown(int num) {
		position.y+=num;
	}

	public void moveLeft(int num) {
		position.x-=num;
	}

	public void moveRight(int num) {
		position.x+=num;
	}
	
	public Block getBlock (int row, int column) {
		return shape.get(row, column);
	}
}
